<?php
namespace app\app\util;
use think\Request;
use think\cache\driver\Redis;

//排行榜有序集合
define('VOTE','wc:vote');
//用户已拥有的球星卡集合
define('STAR_CARD','wc:user_have_card:id=');

class VoteUtils{

	//创建投票数
	public static function foundUserVotes($team_id,$number=0)
	{
		$redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
		//创建用户
		return $redis->zadd(VOTE,$number,$team_id);
	}

	//添加投票数/获取数目
	public static function getUserVotes($team_id,$number=0)
	{
		$redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
		if($number == 0){
			return $redis->zscore(VOTE,$team_id);
		}else{
			$result = $redis->zincrby(VOTE,$number,$team_id);
			return $result;
		}
	}

	//获取指定范围排名
	public static function getVotesRank($start,$end,$type='')
	{
		$redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
		if($type == ''){
			return $redis->zrevrange(VOTE,$start,$end);
		}else{
			$type = 'withscores'; 
			return $redis->zrevrange(VOTE,$start,$end,$type);
		}
	}

	//用户已有球星卡id
	public static function getUserStarCard($user_id,$card_id='')
	{
		$redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
		if($card_id == ''){
			//查询
			return $redis->smembers(STAR_CARD.$user_id);
		}else{
			//添加
			return $redis->sadd(STAR_CARD.$user_id,$card_id);
		}
	}

	//减少用户已有球星卡id
	public static function sremUserStarCard($user_id,$card_id)
	{
		$redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
		return $redis->srem(STAR_CARD.$user_id,$card_id);
	}
}